package com.campusify.service.impl;

import org.springframework.stereotype.Service;
import com.campusify.compents.PageResult;
import com.campusify.pojo.RoleExample;
import com.campusify.pojo.RoleExample.Criteria;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.campusify.mapper.RoleMapper;
import com.campusify.pojo.Role;
import com.campusify.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service
public class RoleServiceImpl implements RoleService {

	@Autowired
	private RoleMapper roleMapper;
	
	/**
	 * 查询全部
	 */
	@Override
	public List<Role> findAll() {
		return roleMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<Role> page=   (Page<Role>) roleMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(Role role) {
		roleMapper.insert(role);		
	}

	
	/**
	 * 修改
	 */
	@Override
	public void update(Role role){
		roleMapper.updateByPrimaryKey(role);
	}	
	
	/**
	 * 根据ID获取实体
	 * @param id
	 * @return
	 */
	@Override
	public Role findOne(Integer roleid){
		return roleMapper.selectByPrimaryKey(roleid);
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Integer[] roleids) {
		for(Integer roleid:roleids){
			roleMapper.deleteByPrimaryKey(roleid);
		}		
	}
	
	
		@Override
	public PageResult findPage(Role role, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		RoleExample example=new RoleExample();
		Criteria criteria = example.createCriteria();
		
		if(role!=null){			
						if(role.getRolename()!=null && role.getRolename().length()>0){
				criteria.andRolenameLike("%"+role.getRolename()+"%");
			}	
		}
		
		Page<Role> page= (Page<Role>)roleMapper.selectByExample(example);		
		return new PageResult(page.getTotal(), page.getResult());
	}
	
}
